--[[
    背包管理
]]
local bagManager = BaseClass("bagManager")

function bagManager : __init(tran)
    -- 查找组件
    self.tran = tran
    local cellCount = 30
    local leftBackground = self.tran.transform : Find("LeftBackground")
    local content = leftBackground.transform : Find("Scroll View/Viewport/Content")
    self.goodsDates = Global.DateBuffers
    -- 实例化背包格子
    for i = 1, cellCount, 1 do
        local cell = GameObject.Instantiate(Resources.Load("Cell/Cell"), content.transform)
        table.insert(Global.BagCells, cell)
    end

    -- 获取所以分类按钮
    local classifitionBtnParent = leftBackground.transform : Find("ClassifictionGroup")
    local allBtn = classifitionBtnParent.transform : Find("BtnAll") : GetComponent(typeof(CS.UnityEngine.UI.Button))
    local equipmentBtn = classifitionBtnParent.transform : Find("BtnEquipment") : GetComponent(typeof(CS.UnityEngine.UI.Button))
    local medicineBtn = classifitionBtnParent.transform : Find("BtnMedicine") : GetComponent(typeof(CS.UnityEngine.UI.Button))
    local boxBtn = classifitionBtnParent.transform : Find("BtnBox") : GetComponent(typeof(CS.UnityEngine.UI.Button))
    table.insert(Global.ClassificationBtns, allBtn)
    table.insert(Global.ClassificationBtns, equipmentBtn)
    table.insert(Global.ClassificationBtns, medicineBtn)
    table.insert(Global.ClassificationBtns, boxBtn)

    print("BagManager输出1：ClassificationBtns".. #Global.ClassificationBtns)

    -- 实例化背包物品
    if #Global.DateBuffers > 0 then
        print("BagManager输出2：缓存区数量".. #Global.DateBuffers)
        print("BagManager输出3：背包格子数量".. #Global.BagCells)
        for i = 1, #Global.DateBuffers, 1 do
            local bagItem = GameObject.Instantiate(Resources.Load("UIItem/BagItem"), Global.BagCells[i].transform)
            require("BagItem").New(bagItem, Global.DateBuffers[i], self.tran)
            table.insert(Global.BagItems, bagItem)
        end
    else
        print("BagManager输出4：背包暂无数据")
    end

    -- 分类
    for i = 1, #Global.ClassificationBtns, 1 do
        local btn = Global.ClassificationBtns[i]
        local typeText = btn.transform:Find("Text"):GetComponent(typeof(CS.UnityEngine.UI.Text)).text
        
        btn.onClick:AddListener(function()
            self:Classification(typeText)  -- 确保这里调用的是大写开头的Classification
        end)
    end
end

function bagManager : Classification(typeText)
    print("BagManager输出5:分类 "..typeText)
    -- 清空现有单元格子
    for _, cell in ipairs(Global.BagCells) do
        local bagItemObj = cell.transform:Find("BagItem(Clone)")
        if bagItemObj then
            GameObject.Destroy(bagItemObj.gameObject)
        end
    end
    
    -- 重新生成背包物品
    local cellIndex = 1 -- 独立的格子索引计数器
    for _, item in ipairs(Global.DateBuffers) do  -- 改为使用 BagItems 数据源
        print("BagManager输出6:重新生成背包物品"..item.type)
        -- 分类判断
        if typeText == "全部" or item.type == typeText then
            local bagItem = GameObject.Instantiate(
                Resources.Load("UIItem/BagItem"),
                Global.BagCells[cellIndex].transform
            )
            require("BagItem").New(bagItem, item, self.tran)
            cellIndex = cellIndex + 1
        end
    end
end

function bagManager : RefreshBag()
    -- 清空现有背包格子
    for _, cell in ipairs(Global.BagCells) do
        GameObject.Destroy(cell.transform:Find("BagItem").gameObject)
    end
    
    -- 重新生成背包物品
    for i, item in ipairs(Global.BagItems) do
        if Global.BagCells[i] then
            local bagItem = GameObject.Instantiate(Resources.Load("UIItem/BagItem"), Global.BagCells[i].transform)
            require("BagItem").New(bagItem, item, self.tran)
        end
    end
end

return bagManager